<?php
/**
 * This file is part of the UXLabs project.
 *
 * (c) UXLabs project <http://code.uxlabs.me>
 *
 */

namespace uxl\app\api\components;

use Yii;
use yii\base\Object;

class BaseApiMethod extends Object implements IApiMethod
{
    /**
     * @var Object 处理后结果
     */
    protected $result;
    /**
     * @var int 处理结果状态值
     */
    protected $status = ResultStatus::SUCCESS;
    /**
     * @var string 错误信息
     */
    protected $errorMsg;
    /**
     * @var array 业务信息
     */
    protected $errorData = [];
    /**
     * @var array 访问授权
     */
    protected $accessToken;
    /**
     * @var array|null 请求的参数
     */
    protected $paramData;
    /**
     * @var bool 是否是游客
     */
    protected $isGuest = true;
    /**
     * @var bool 是否验证登录
     */
    public $isVerifyLogin = false;
    /**
     * @var array 请求的业务参数
     */
    protected $data = null;
    /**
     * @var null|int|string 登录用户的ID
     */
    protected $userId;

    /**
     * 获取处理结果
     * @return Object
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * 获取处理结果状态
     * @return int
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * 获取处理错误信息
     * @return string
     */
    public function getErrorMsg()
    {
        return $this->errorMsg;
    }

    /**
     * 设置访问密钥信息
     * @param $accessToken
     */
    public function setAccessToken($accessToken)
    {
        $this->accessToken = $accessToken;
    }

    /**
     * 设置请求的业务数据
     * @param $data
     */
    public function setParamData($data)
    {
        $this->paramData = $data;
    }

    /**
     * 错误日志
     * @param $message
     */
    protected function errorLog($message)
    {
        Yii::error($message, 'appApi');
    }

    /**
     * 调试日志
     * @param $message
     */
    protected function traceLog($message)
    {
        Yii::trace($message, 'appApi');
    }

    /**
     * 验证用户登录信息
     * @return bool
     */
    protected function verifyUserAuth()
    {
        /*
         * if(empty($this->parameters->userToken)) {
            $this->isGuest = true;
            if($this->verifyLogin) {
                $this->status = ResultStatus::USER_TOKEN_ERROR;
                $this->error = '用户授权错误';
            }
            return false;
        }
        //加入活动
        try{
            $userAuth = AppApiDao::userAuthInfo($this->accessToken->id, $this->parameters->userToken);
        }catch(\Exception $e){
            $userAuth = AppApiDao::userTokenInfo($this->parameters->userToken);
        }
        if($userAuth == false || $userAuth->state != 1) {
            $this->isGuest = true;
            if($this->verifyLogin) {
                $this->status = ResultStatus::USER_TOKEN_EXPIRES;
                $this->error = '用户授权失效';
            }
            return false;
        }
        $this->isGuest = false;
        $this->userId = $userAuth->user_id;
        return true;
         */
        // TODO 登录验证
    }

    /**
     * 保存用户授权信息
     * @param $userId
     * @return bool|string user token
     */
    protected function saveUserAuth($userId)
    {
        /*
         * $token = Utils::createToken();
        if(AppApiDao::addUserAuth($this->accessToken->id, $userId, $token)) {
            return $token;
        }
        return false;
         */
        // TODO 保存用户授权成功信息
    }

    /**
     * 执行
     */
    public function runAction()
    {
        if ($this->status != ResultStatus::SUCCESS) {
            return;
        }
        $this->verifyUserAuth();
        // 验证登录
        if ($this->isVerifyLogin && $this->isGuest) {
            return;
        }
        ob_start();
        $result = $this->run();
        $out = ob_get_clean();
        if ($result) {
            $this->result = $result;
        } elseif (empty($out) == false) {
            $this->result = $out;
        } else {
            $this->result = is_array($result) ? [] : '';
        }
    }

    public function run()
    {
        throw new \Exception('null method');
    }
}